<include file="Public/header"/>
<include file="Public/menu"/>
<style>
    input[name=num] {
        width:50px;
    }
</style>
<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            店铺
            <small>佣金设置</small>
        </h1>
        <ol class="breadcrumb">
            <li><a><i class="fa fa-dashboard"></i> 店铺</a></li>
            <li><a href="{:U('Qgs/Supplier/index')}"><i class="fa fa-dashboard"></i> 店铺管理</a></li>
            <li class="active">佣金设置</li>
        </ol>
    </section>

    <!-- 内容 -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <form action="/admin/rbac/roleAddMenu/id/3" method="post">
                        <!-- /.box-header -->
                        <div class="box-body table-responsive no-padding">
                            <table class="table table-hover">
                                <tbody>
                                <tr>
                                    <th></th>
                                    <th>商品分类</th>
                                    <th>操作</th>
                                </tr>


                                <input type="hidden" name="id" value="3">
                                <volist name="list" id="vo">
                                    <tr>
                                        <td></td>
                                        <td>
                                            {$vo.name}
                                        </td>
                                        <?php if(isset($vo['num'])) {;?>
                                        <td>
                                            <input type="text" name="num" min="0" value="<?php echo $vo['num'];?>">%
                                            <a href="javascript:;" data-id="<?php echo $vo['id'];?>" class="btn btn-sm btn-warning"><i class="fa">保存</i></a>
                                        </td>
                                        <?php }?>
                                    </tr>
                                    <if condition="count($vo['store_cat_id']) gt 0">
                                        <volist name="vo['store_cat_id']" id="v">
                                            <tr>
                                                <td></td>
                                                <td>
                                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$v.name}
                                                </td>
                                                <?php if(isset($v['num'])) {;?>
                                                <td>
                                                    <input type="text" name="num" min="0" value="<?php echo $v['num'];?>">%
                                                    <a href="javascript:;" data-id="<?php echo $v['id'];?>" class="btn btn-sm btn-warning"><i class="fa">保存</i></a>
                                                </td>
                                                <?php }?>
                                            </tr>
                                            <if condition="count($v['store_cat_id']) gt 0">
                                                <volist name="v['store_cat_id']" id="item">
                                                <tr>
                                                    <td></td>
                                                    <td>
                                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$item.name}
                                                    </td>
                                                    <?php if(isset($item['num'])) {;?>
                                                    <td>
                                                        <input type="text" name="num" min="0" value="<?php echo $item['num'];?>">%
                                                        <a href="javascript:;" data-id="<?php echo $item['id'];?>" class="btn btn-sm btn-warning"><i class="fa">保存</i></a>
                                                    </td>
                                                    <?php }?>
                                                </tr>
                                                    <?php if(count($item['store_cat_id'])) {;?>
                                                        <?php foreach($item['store_cat_id'] as $i) {;?>
                                                            <tr>
                                                                <td></td>
                                                                <td>
                                                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $i['name'];?>
                                                                </td>
                                                                <?php if(isset($i['num'])) {;?>
                                                                <td>
                                                                    <input type="text" name="num" min="0" value="<?php echo $i['num'];?>">%
                                                                    <a href="javascript:;" data-id="<?php echo $i['id'];?>" class="btn btn-sm btn-warning"><i class="fa">保存</i></a>
                                                                </td>
                                                                <?php }?>
                                                            </tr>
                                                        <?php };?>
                                                    <?php };?>
                                                </volist>
                                            </if>
                                        </volist>
                                    </if>
                                </volist>

                                </tbody>
                            </table>
                        </div>
                        <div class="box-footer clearfix">
                            <a class="btn btn-sm btn-warning batch-save">全部保存</a>
                            <a class="btn btn-sm btn-primary grid-refresh"><i class="fa fa-refresh"></i></a>
                            {$page}
                        </div>
                    </form>
                </div>
            </div>
        </div>

    </section>
</div>
<!-- 底部 -->
<include file="Public/common_footer"/>

<script data-exec-on-popstate>

    $(function () {

        $('.grid-select-all').change(function() {
            if (this.checked) {
                $('.grid-item').prop("checked", true);
            } else {
                $('.grid-item').prop("checked", false);
            }
        });

        var ff = false;
        function controlMnm(i) {
            var shu = i.value;
            var min = i.min;
            if (shu < min) {
                alert('佣金比例不能小于'+min);
                ff = true;
            }
        }

        $('td .btn-warning').click(function () {
            var input = $(this).prev('input[name=num]');
            controlMnm(input[0]);
            if (ff) {
                return false;
            }
            var cat_id = $(this).attr('data-id');
            var num = $(this).prev('input[name=num]').val();
            if(!isNaN(num)){

            }else{
                alert("请正确填写");
                return false;
            }
            if (cat_id) {
                $.ajax({
                    type: "POST",
                    url: "{:U('Supplier/saveCommission', array('local_id' => $local_id), '')}",
                    data: {cat_id:cat_id, num:num},
                    success: function(msg){
                        alert(msg.message);
                        window.location.reload();
                    },
                    error: function (msg) {
                        alert(msg.message);
                    }
                });
            }
        });

        $('.batch-save').on('click', function() {
            var selected = [];
            $('input[name=num]').each(function(){
                controlMnm(this);
                var row = [];
                var num = $(this).val();
                var cat_id = $(this).next('a').attr('data-id');
                row = {num:num, cat_id:cat_id};
                selected.push(row);
                if(isNaN(num)){
                    alert("请正确填写");
                    return false;
                }
            });

            if (ff) {
                return false;
            }
            if (selected.length == 0) {
                return false;
            }

            if(confirm("确定批量保存 ?")) {
                $.ajax({
                    type: "POST",
                    url: "{:U('Supplier/saveCommission',array('local_id' => $local_id), '')}",
                    data: {cat_id:selected},
                    success: function(msg){
                        alert(msg.message);
                        window.location.reload();
                    },
                    error: function (msg) {
                        alert(msg.message);
                    }
                });
            }
        });
    });
</script>